<?php
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: 2/25/2012
 * @project		: CodeGenerater
 * @description	:
 *		- Generate content files: form_validation, controller, view
 *
\* ************************************************** */


    /* ------------------------------------------------------------ *\
    | - FUNC Purpose	: Generate content for file form_validation.php
    | - Parameters		: 
    |		Name			Type		Description
    |		---------------------------------------------------
    |		$option         Array       
    |								
    | - Return			: 
    |			- Content file validation.
    \* ------------------------------------------------------------ */
    function generate_content_validation_file( $option = array( ) ){
        $form_add_data              =   implode( ',' . "\n\t\t\t\t\t\t\t\t", $option[ 'form_val_add_data' ]);
        $form_validation_add_data   =   "'".$option[ 'tablename' ]."_add' => array(" . $form_add_data . ")";
                
        if(isset($option[ 'form_val_edit_data' ])){
            $form_edit_data              =   implode( ',' . "\n\t\t\t\t\t\t\t\t", $option[ 'form_val_edit_data' ]);
            $form_validation_edit_data   =   "\n\n\t\t\t\t'".$option[ 'tablename' ]."_edit' => array(" . $form_edit_data . ")";
        }

        if( file_exists( $option[ 'file_validation' ] ) ){
            $form_v     =   file_get_contents( $option[ 'file_validation' ] );
            
            $old_form   =   str_replace( array( '<?php','?>', '$config = array(', ');'), '', $form_v ) . "\t\t\t\t,\n\n\t\t\t\t";

            include( $option[ 'file_validation' ] );
            
            if( isset( $config[ $option[ 'tablename' ] ] ) ){
                // Rules already existed , reload rules
                // --------------------------------------------------
                if(isset($option[ 'form_val_edit_data' ])){
                    $form_content = str_replace('{form_validation_add_data}',
                                     $form_validation_add_data.",".$form_validation_edit_data, file_get_contents( 'templates/form_validation.php' ) );
                }else{
                    $form_content = str_replace( '{form_validation_add_data}',
                                     $form_validation_add_data, file_get_contents( 'templates/form_validation.php' ) );
                }
            }else{
                // Append new rule
                // --------------------------------------------------
                if(isset($option[ 'form_val_edit_data' ])){
                    $form_content = str_replace( '{form_validation_add_data}', 
                                    $form_validation_add_data.",".$form_validation_edit_data, file_get_contents( 'templates/form_validation.php' ) );
                }else{
                    $form_content = str_replace( '{form_validation_add_data}', 
                                    $form_validation_add_data, file_get_contents( 'templates/form_validation.php' ) );
                }
            }
        }else{	
            if(isset($option[ 'form_val_edit_data' ])){
                $form_content = str_replace( '{form_validation_add_data}', 
                                    $form_validation_add_data.",".$form_validation_edit_data, file_get_contents( 'templates/form_validation.php' ) );
            }else{
                $form_content = str_replace( '{form_validation_add_data}', 
                                    $form_validation_add_data, file_get_contents( 'templates/form_validation.php' ) );                
            }
        }
  
        return $form_content;
    }


    /* ------------------------------------------------------------ *\
    | - FUNC Purpose	: Generate content for file {controller}.php
    | - Parameters		: 
    |		Name			Type		Description
    |		---------------------------------------------------
    |		$option         Array 
    |								
    | - Return			: 
    |			- 
    \* ------------------------------------------------------------ */
    function generate_content_controller_file( $option = array( ) ){
        $controller     =   file_get_contents( $option[ 'file_temp_controller' ] );
        $search         =   array( 
        '{controller_name}', '{view}', '{table}','{validation_add_name}','{validation_edit_name}',
        '{data}','{edit_data}','{controller_name_l}','{primaryKey}','{master_view}','{fields_list}',
        '{master_limited}','{action}', '{custom_action}' );
        
        $replace = array(
                    ucfirst( $option[ 'controller' ] ), 
                    $option[ 'view' ],
                    $option[ 'table' ],
                    $option[ 'validation_add' ],
                    $option[ 'validation_edit' ],
                    implode( ','."\n\t\t\t\t\t", $option[ 'controller_form_data' ] ),
                    implode( ','."\n\t\t\t\t\t", is_array( $option[ 'controller_form_editdata' ] ) ? $option[ 'controller_form_editdata' ] : array( ) ),
                    $option[ 'controller' ],
                    $option[ 'primaryKey' ],
                    $option[ 'master_view'],
                    $option[ 'fields_combine' ],
                    implode( ',', $option[ 'master_limited' ] ),
                    $option[ 'action' ],
                    $option[ 'custom_action'],
                    );
        $controller_content = str_replace( $search, $replace, $controller );
        return $controller_content;        
    }

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Generate content for file {name}_list.php
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$option         Array
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function generate_content_list_form( $option = array( ) ){
        $list_v         =   file_get_contents( $option[ 'file_temp_list' ] );
        $list_content   =   str_replace( $option[ 'controller_name_l' ], $option[ 'controller' ], $list_v );
        
        return $list_content;
    }

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Generate content for file {name}_add.php
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$option         Array
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function generate_content_add_form( $option = array( ) ){
        $add_v          =   file_get_contents( $option[ 'file_temp_add' ] );
        $add_content    =   str_replace( $option[ 'forms_inputs' ], implode( "\n", $option['add_form']), $add_v );
        
        return $add_content;
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Generate content for file {name}_edit.php
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$option         Array
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function generate_content_edit_form( $option = array( ) ){
        $edit_v         =   file_get_contents( $option[ 'file_temp_edit' ] );
        $edit_search    =   array( $option[ 'forms_inputs' ], $option[ 'primary' ] );
        $edit_replace   =   array( implode( "\n", $option[ 'edit_form' ] ), '<?php echo form_hidden(\'' . $option[ 'primaryKey' ] . '\',$result->' . $option[ 'primaryKey' ].') ?>' );
        $edit_content   =   str_replace( $edit_search, $edit_replace, $edit_v );
        
        return $edit_content;
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Write file to save.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$file           string      file name
     |      $content        string      content write to file
     |      								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function writefile( $file, $content){   
        if ( !write_file( $file, $content ) ){
            return $file. ' - Unable to write the file';
        } else{
            return false;
        }
    }
?>